﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for HLTVV06.xaml
    /// </summary>
    public partial class HLTVV06 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        HLTVV06Data m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        HLTVV06Report m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        // string OrderNumber="";
        
        #endregion
        public HLTVV06()
        {
            InitializeComponent();
            this.Name = "发货单";
        }
        public override void SetUp (Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           
            
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new HLTVV06Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }
        private void InitDataBinding()
        {
            this.setUI.Tables.Add("VKPOS");
            this.setUI.Tables.Add("VVTLIEF");
            this.setUI.Tables.Add("COLOR");
            this.setUI.Tables.Add("WBBST");

            listVV06=new ArrayList();
            ListColor=new ArrayList();
            listBatchNo = new ArrayList();
            orlReadCommand = OracleDB.CreateCommand();

            #region "VKPOS"
            orlReadCommand.CommandText = "SELECT B.KDKUNDNA,A.VKPAUFKRS,A.VKPAUFLFD,A.VKPAUFPOS,A.VKPART,"
                                                  + " A.VKPFARBE,A.VKPGROE,round(A.VKPMENGE1/100,3) AS QUANTITY,A.VKPMEEH,"
                                                  + " A.VKPKDBEZ,A.VKPKDARTNR,A.VKPKDKOMM,A.VKPZUSATZ"
                                                + " FROM VKPOS A,KDSTAMM B  WHERE A.VKPMANDANT = B.KDMANDANT "
                                                + " AND A.VKPKUNDNR = B.KDKUNDNR AND RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "VKPOS";
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion

            #region "VVTLIEF"
            orlReadCommand.CommandText = "SELECT VVTLIEFSCH,VVTLIEFSCHPOS,VVSTATUS,VVVERSART,VVSPED,VVTRENR,"
                                        + " VVAVISNR,VVVERSDAT,round(VVTMTR/100/0.9144,3) AS QUANTITY,VVTKG/1000 AS KG,VVTAUFKRS,VVTAUFLFD,VVTAUFPOS "
                                        + " FROM VVTLIEF A,VVLIEF B WHERE A.VVTMANDANT = B.VVMANDANT "
                                        + " AND A.VVTLIEFSCH = B.VVLIEFSCH AND vvtmandant = '00' AND RowNum<1";
            //decode(VVSTATUS,'LI','信用限制','KO','发货单不完整','EN','提货单未打印','VF','未发货','VS','已出货未产生发票','IK','数据未完全确认','SP','锁定','VR','已产生发票',VVSTATUS) AS STATUS,
            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "VVTLIEF";

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    this.setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }

            }
            #endregion

            #region "COLOR"
            orlReadCommand.CommandText = "select substr(sdkey,3,3) as COLOR,substr(sddaten,2,10) as DES "
                                        + " from sddat "
                                        + " where sdsa = '0201' and substr(sdkey,1,2) = '00' AND substr(sdkey,24,1)= 'V'AND RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "COLOR";

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    this.setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }

            }
            #endregion

            #region "WBBST"
            orlReadCommand.CommandText = "SELECT rownum AS CODE,WBBALIEFSCH,WBBAUFKRS,WBBAUFLFD,WBBAUFPOS"
                + ",WBBRWKRS,WBBRWLFD,WBBPOS,WBBSTKNR,WBBWAKZ,WBBDPART"
                + ",WBBART,WBBVAR,WBBFARBE,WBBBESCHICH,WBBSSTKLG/0.9144 AS WBBSSTKLG,WBBSGEW,WBBSBRT,WBBTAX,WBBBEM"
                + " FROM WBBST W WHERE   WBBMANDANT = '00' AND RowNum<1 ";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "WBBST";

                foreach (DataTable dtTemp in dsResult.Tables)
                {

                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion


            setUI.AcceptChanges();
            this.VKPOSDataGrid.DataContext = setUI;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            btnUpdate_Click(null, null);
  
        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (DataTable dtTemp in setUI.Tables)
            {
                if (dtTemp.TableName != "VKPOS")
                {
                    dtTemp.Clear();
                }
            }
            setUI.AcceptChanges();
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVKPOS = drv.Row;
                this.txtVKPAUFLFD.Text = dwVKPOS["VKPAUFKRS"].ToString() + dwVKPOS["VKPAUFLFD"].ToString();
                Tracer.Speak("销售单："+txtVKPAUFLFD.Text);
                ReloadVVTLIEF(dwVKPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        } 
        private void ReloadVVTLIEF(DataRow dwVKPOS)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBAUFKRS"] = dwVKPOS["VKPAUFKRS"];
            dwWBBST["WBBAUFLFD"] = dwVKPOS["VKPAUFLFD"];
            dwWBBST["WBBAUFPOS"] = dwVKPOS["VKPAUFPOS"];
            DataSet setResult=m_Data.GetVVTLIEF(dwWBBST);
            setUI.Tables["VVTLIEF"].Clear();
            setUI.Merge(setResult);
        }
        private void listVVTLIEF_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVVLIEFSCH = drv.Row;
                this.txtVVLIEFSCH.Text = dwVVLIEFSCH["VVTLIEFSCH"].ToString(); 
                ReloadWBBST(dwVVLIEFSCH);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ReloadWBBST(DataRow dwVVLIEFSCH)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBAUFKRS"] = dwVVLIEFSCH["VVTAUFKRS"];
            dwWBBST["WBBAUFLFD"] = dwVVLIEFSCH["VVTAUFLFD"];
            dwWBBST["WBBAUFPOS"] = dwVVLIEFSCH["VVTAUFPOS"];
            dwWBBST["WBBALIEFSCH"] = dwVVLIEFSCH["VVTLIEFSCH"];
            DataSet setResult = m_Data.GetWBBST(ref listVV06, ref ListColor, ref listBatchNo,dwWBBST);
            string conclusion="共有";
            int count = 0;
            foreach (string sTemp in ListColor)
            {
                conclusion += sTemp + ",";
                count++;
            }
            conclusion += count + "个色级。"+ Environment.NewLine;;
            count = 0;
            conclusion += "共有";
            foreach (string sTemp in listBatchNo)
            {
                conclusion += sTemp+",";
                count++;
            }
            conclusion += count + "缸。" + Environment.NewLine;
            conclusion += "共分";
            count = 0;
            foreach (string sTemp in listVV06)
            {
                conclusion += sTemp + "、";
                count++;
            }
            conclusion += count + "次运输。" + Environment.NewLine; 
            txtConslusion.Text= conclusion;
            Tracer.Speak(conclusion);

            setUI.Tables["WBBST"].Clear();
            setUI.Merge(setResult);
            listWBBST.DataContext = setUI;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                DataRow dwTmp;
                DataSet setRpt = new DataSet();
                listVV06 = new ArrayList();
                string Customer = "";
                string Address = "";
                ListColor = new ArrayList();
                listBatchNo = new ArrayList();
                setRpt = m_Data.BuildData(ref listVV06, ref ListColor, ref listBatchNo, dwWBBST);
                if (setRpt.Tables["WBBST"].Rows.Count > 0)
                {
                    dwTmp = setRpt.Tables["WBBST"].Rows[0];
                    dicParamater["WBBAUFKRS"] = dwTmp["WBBAUFKRS"].ToString();
                    dicParamater["WBBAUFLFD"] = dwTmp["WBBAUFLFD"].ToString();
                    dicParamater["WBBAUFPOS"] = dwTmp["WBBAUFPOS"].ToString();
                    dicParamater["WBBALIEFSCH"] = dwTmp["WBBALIEFSCH"].ToString();
                    dicParamater["WBBTAX"] = dwTmp["WBBTAX"].ToString();
                    Customer = setRpt.Tables["VKPOS"].Rows[0]["KDNAME1"].ToString();
                    Address = setRpt.Tables["VVTLIEF"].Rows[0]["VVORT"].ToString();
                    m_DataX = new HLTVV06Data(dicParamater);
                    m_DataX.OracleDB = this.OracleDB;
                    m_DataX.proxy = this.proxy;
                    m_rpt = new HLTVV06Report(dicParamater);
                    m_rpt.listBatchNo = listBatchNo;
                    m_rpt.ListColor = ListColor;
                    m_rpt.listVV06 = listVV06;
                    m_rpt.Customer = Customer;
                    m_rpt.Address = Address;
                    m_rpt.dsSource = setRpt;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DataX.exAgent;
                    m_rpt.GetHLTVV06Report("EXCEL");

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));
                    InitDataBinding();
                    setResult = m_Data.LoadVKPOS();
                    setUI.Clear();
                    setUI.Merge(setResult);
                    this.DataContext = setUI;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
    }
}
